package com.zmm.weblog.common.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.zmm.weblog.common.entity.DynamicEntity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class DynamicDataListener extends AnalysisEventListener<Map<Integer, String>> {
    private List<DynamicEntity> entityList = new ArrayList<>();

    @Override
    public void invoke(Map<Integer, String> data, AnalysisContext context) {
        DynamicEntity entity = new DynamicEntity();
        for (Map.Entry<Integer, String> entry : data.entrySet()) {
            String header = context.readSheetHolder().getExcelReadHeadProperty().getHeadMap().get(entry.getKey()).getHeadNameList().get(0);
            entity.set(header, entry.getValue());
        }
        entityList.add(entity);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        // All data is read
    }

    public List<DynamicEntity> getEntityList() {
        return entityList;
    }
}